#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <math.h>
#include "tw2815.h"

//#define TW2815_SET_VIDEO_MODE	0x17
//#define TW2815_GET_VIDEO_MODE	0x33
//#define TW2815_REG_DUMP  0x34
//#define TW2815_REG_DUMP  _IOWR('T', 35,int)
//printk(" TW2815_SATURATION_CONTROL : ch = 0x%02x control = 0x%02x \n", tw2815_controlvalue.ch,tw2815_controlvalue.controlvalue);

int main()
{
	int dev;
	unsigned long on;
	char buf[4096];
	FILE *fp;
	int count = 1;
	int tmp = 0;
	char mode[8];
	int status;

	tw2815_w_reg tw2815reg;
    tw2815_set_2d1 tw2815_2d1;
    tw2815_set_videomode tw2815_videomode;
    tw2815_set_controlvalue tw2815_controlvalue;

	dev = open("/dev/misc/tw2815adev", O_RDONLY);
	if (dev <= 0) {
		printf("open fail\n");
		exit(0);
	}

	// status .
	status = -1;
	ioctl(dev,TW2815_STATE_OF_VIDEO_DETECTION, &status);
	printf("status is = 0x%02x\n", status);
	status = status & 0xf;
	switch(status) {
		case 1:
		case 4:
			break;
		case 2:
		case 8:
			break;
	}

	
/*	on = 1;	
	ioctl(dev, TW2815_GET_VIDEO_MODE, &on);
	printf("TW2815_GET_VIDEO_MODE on = %d\n", on);

	if (on == 2 )
		printf("mode = %s\n", "PAL");
	if (on == 1 )
		printf("mode = %s\n", "NTSC");
	else
		printf("mode = %s\n", "AUTO");
*/
	/* MODE */
	tw2815_videomode.mode = 1;	// 0 : auto  1 : ntsc  2 : pal	 four Channel
	ioctl(dev, TW2815_SET_VIDEO_MODE, &tw2815_videomode);

	/* SATURATION */
	tw2815_controlvalue.ch = 0;		// channel				
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_SATURATION_CONTROL, &tw2815_controlvalue);

	tw2815_controlvalue.ch = 1;						
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_SATURATION_CONTROL, &tw2815_controlvalue);

	tw2815_controlvalue.ch = 2;						
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_SATURATION_CONTROL, &tw2815_controlvalue);

	tw2815_controlvalue.ch = 3;						
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_SATURATION_CONTROL, &tw2815_controlvalue);

	/* HUE */
	tw2815_controlvalue.ch = 0;		// channel				
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_HUE_CONTROL, &tw2815_controlvalue);

	tw2815_controlvalue.ch = 1;						
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_HUE_CONTROL, &tw2815_controlvalue);

	tw2815_controlvalue.ch = 2;						
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_HUE_CONTROL, &tw2815_controlvalue);

	tw2815_controlvalue.ch = 3;						
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_HUE_CONTROL, &tw2815_controlvalue);

	/* BRIGHTNESS */
	tw2815_controlvalue.ch = 0;		// channel				
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_BRIGHTNESS_CONTROL, &tw2815_controlvalue);

	tw2815_controlvalue.ch = 1;					
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_BRIGHTNESS_CONTROL, &tw2815_controlvalue);

	tw2815_controlvalue.ch = 2;						
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_BRIGHTNESS_CONTROL, &tw2815_controlvalue);

	tw2815_controlvalue.ch = 3;					
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_BRIGHTNESS_CONTROL, &tw2815_controlvalue); 
	
	/* CONTRAST */
	tw2815_controlvalue.ch = 0;		// channel				
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_CONTRAST_CONTROL, &tw2815_controlvalue);

	tw2815_controlvalue.ch = 1;					
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_CONTRAST_CONTROL, &tw2815_controlvalue);

	tw2815_controlvalue.ch = 2;						
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_CONTRAST_CONTROL, &tw2815_controlvalue);

	tw2815_controlvalue.ch = 3;					
	tw2815_controlvalue.controlvalue = 0x80;	
	ioctl(dev, TW2815_CONTRAST_CONTROL, &tw2815_controlvalue);


#if 0
	ioctl(dev, TW2815_SET_D1);		// setd1

	tw2815_2d1.ch1 = 0;
	tw2815_2d1.ch2 = 1;
	ioctl(dev, TW2815_SET_2_D1, &tw2815_2d1);	// set_2_d1
#endif
	ioctl(dev, TW2815_REG_DUMP);
	
	/*
	if ( fp = fopen("/dvs/vs/video.dat", "w"))
		printf("-------- file open sucess! --------\n");
	else {
		printf("-------- file open fail! --------\n");
		exit(-1);
	}

	while (count <= 5) {
		memset(buf, 0, sizeof buf);
		tmp = read(dev, buf, sizeof buf);
		if (tmp != sizeof(buf)) {
			printf("read wrong number of bytes = %d\n", tmp);
			//exit(-1);
		}
		fwrite(buf, sizeof(buf), 1, fp);
		count++;
	}
	fclose(fp);*/
	close(dev);

	return 0;
}
